<?php

namespace App\Models;

use App\Services\LbsService\LbsService;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

/**
 * App\Models\Member
 *
 * @property int $id
 * @property string|null $openid openid
 * @property string|null $cookie cookie
 * @property string|null $name 姓名
 * @property int $is_guest 是否访客
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property array|null $setting 设置
 * @property string|null $phone 手机号码
 * @property int $is_authenticated_phone 手机验证
 * @property string|null $address 地址
 * @property string|null $address_geo geo
 * @property string|null $nickname 昵称
 * @property string|null $avatar 头像
 * @property string|null $email 邮件
 * @property int|null $sex 性别
 * @property string|null $city 城市
 * @property string|null $province 省份
 * @property int $is_authorized 是否已经授权
 * @property string|null $address_lat 纬度
 * @property string|null $address_lng 经度
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\BlackList[] $black_lists
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Favorite[] $favorites
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\History[] $histories
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\MemberPortrayal[] $portrayals
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Tagged[] $tags
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAddressGeo($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAddressLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAddressLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereCookie($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereIsAuthenticatedPhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereIsAuthorized($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereIsGuest($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereSetting($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Member whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Member extends Authenticatable implements JWTSubject
{
    use Notifiable;

    protected $casts      = ['setting' => 'json'];
    protected $fillable   = ['cookie', 'openid', 'name', 'nickname', 'avatar', 'email', 'sex', 'city', 'province', 'address'];
    protected $attributes = [
        'setting' => '{}',
    ];

    public static function boot()
    {
        parent::boot();
        self::saving(function (Member $member) {
            $dirty = $member->getDirty();
            if (isset($dirty['address'])) {
                $lbs                 = new LbsService();
                $geo                 = $lbs->address("广东省云浮市新兴县" . $member->address);
                $member->address_geo = $geo->toGeo();
                list($lat, $lng) = $geo->toPoint();
                $member->address_lat = $lat;
                $member->address_lng = $lng;
            }
        });
    }

    // Rest omitted for brevity

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function favorites()
    {
        return $this->hasMany(Favorite::class);
    }

    public function histories()
    {
        return $this->hasMany(History::class);
    }

    public function black_lists()
    {
        return $this->hasMany(BlackList::class);
    }

    public function tags()
    {
        return $this->morphMany(Tagged::class, 'module');
    }

    public function portrayals()
    {
        return $this->hasMany(MemberPortrayal::class);
    }
}
